package com.lwq.util;

import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2021/6/22
 */
public class PageUtil {

    public static final String ASC = "asc";

    public static final String DESC = "desc";

    /**
     * 初始化jpa 排序参数
     * @param orderBy
     * @param direction
     * @return
     */
    public static Sort sortInit(String[] orderBy, String direction){
        Sort sort = null;
        if(!StringUtils.isEmpty(orderBy) &&
            !StringUtils.isEmpty(direction)){
            sort = Sort.by(ASC.equals(direction)?Sort.Direction.ASC:Sort.Direction.DESC,orderBy);
            return sort;
        }

        if(!StringUtils.isEmpty(orderBy) &&
            StringUtils.isEmpty(direction)){
            sort = Sort.by(ASC.equals(direction)?Sort.Direction.ASC:Sort.Direction.DESC);
            return sort;
        }

        if(!StringUtils.isEmpty(direction) &&
            StringUtils.isEmpty(orderBy)){
            sort = Sort.by(direction);
            return sort;
        }

        return null;
    }
}
